package com.qf.Dao.Impl;/*
 *
 */

import com.qf.Dao.GoodsTypeDao;
import com.qf.Domain.GoodsType;
import com.qf.Utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GoodsTypeDaoImpl implements GoodsTypeDao {
    private QueryRunner qr=new QueryRunner();
    @Override
    public List<GoodsType> select(Integer level) {
        String sql="select * from tb_goods_type where level=?";
        Connection conn = DataSourceUtils.getConnection();
        try {
            return qr.query(conn,sql,new BeanListHandler<>(GoodsType.class),level);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public GoodsType selectById(Integer typeid) {
        String sql="select * from tb_goods_type where id=?";
        Connection conn = DataSourceUtils.getConnection();
        try {
            return qr.query(conn, sql, new BeanHandler<>(GoodsType.class), typeid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }
}
